package info.zlong.visibleruntime.core.util;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;

public class AddressUtils {
    private static final String HOST_DELIMINITER = "|";
    
	/**
	 * 解析hosts
	 * @param serialzedHosts 192.168.1.100:9300|192.168.1.101
	 * @param defaultPort
	 * @return
	 */
	public static List<InetSocketAddress> parseAdress(String serialzedHosts,int defaultPort){
		List<InetSocketAddress> result  = new ArrayList<InetSocketAddress>();
        String[] hosts = serialzedHosts.split("[" + HOST_DELIMINITER + "]");
        for (String host : hosts) {
            if (!host.isEmpty()) {
            	
                String[] brokenUp = host.split(":");
                InetSocketAddress inetSocketAddress = new InetSocketAddress(brokenUp[0], brokenUp.length == 2 ? Integer.parseInt(brokenUp[1]) : defaultPort);
                result.add(inetSocketAddress);
            }
        }
        return result;
	}

}
